<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * role.php
 * @author ranuandra
 *
 * 15 Mei 2012 08:14:58
 *
 * 
 */


class Controller_Admin_Role extends Kohana_Admin
{
	public function action_index()
	{
		$roles = ORM::factory('role')->find_all();
		$view = View::factory('admin/role/index');
		$view->datas = $roles;
		
		$this->template->container = $view;
	}
	
	public function action_new()
	{
		$form = array('name' => '', 'description' => '');
		$view = View::factory('admin/role/form');
		$view->form = $form;
		
		$this->template->container = $view;
	}
	
	public function action_edit()
	{
		$id = Request::current()->param('id');
		if($id != "")
		{
			$form = ORM::factory('role', $id);
			$view = View::factory('admin/role/form');
			$view->form = $form;
			
			$this->template->container = $view;			
		}
	}
	
	public function action_save()
	{

		if(isset($_POST['submit']))
		{
			$db = Database::instance();
			$db->begin();
			
			try 
			{
				if(isset($_POST['id']))
				{
					$role = ORM::factory('role', Request::current()->post('id'));
					$role->name = Request::current()->post('name');
					$role->description = Request::current()->post('description');
					$role->update();
				}
				else 
				{
					$role = ORM::factory('role');
					$role->name = Request::current()->post('name');
					$role->description = Request::current()->post('description');
					$role->save();
				}
				
				$db->commit();
				Request::current()->redirect(URL::base().'admin/role');
				exit;
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);
			}
		}		
	}	
	
}